package com.oa.controller;


import java.sql.Timestamp;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;


import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.oa.bean.Bored;
import com.oa.bean.Leaves;
import com.oa.bean.Orders;
import com.oa.bean.Privilege;
import com.oa.bean.Reply;
import com.oa.bean.Role;
import com.oa.bean.RolePrivilege;
import com.oa.bean.Send;
import com.oa.bean.User;
import com.oa.bean.UserAndSection;
import com.oa.service.ApplyService;
import com.oa.service.PostService;
import com.oa.service.SealService;
import com.oa.service.SystemService;
import com.opensymphony.xwork2.util.finder.ClassFinder.PackageInfo;


@Controller
public class ApplyController {

	@Resource
	private ApplyService applyService;
	@Resource
	private SealService sealService;
	@Resource
	private PostService postService;
	@Resource
	private SystemService systemServices;
	@RequestMapping("/queryByIdName")
	@ResponseBody
	public Map<String,Object> queryByIdName(){
		System.out.println("999999");
		
		User name = applyService.queryByIdName(10);
		Map<String, Object> map=new HashMap<>();
		map.put("user", name);
		System.out.println(map+"map"+name.getDeptname());
		return map;
	}

	//审批人下拉框
	@RequestMapping("/sectionList")
	@ResponseBody
	public Map<String,Object> sectionList(int uid) {
		
		List<UserAndSection> sectionList = applyService.sectionList(uid);
		Map<String, Object> map=new HashMap<>();
		map.put("section", sectionList);
		return map;
	}
	
	//新增申请请假
	@RequestMapping("/addLeaves")
	public String addLeaves(Leaves lea,HttpServletRequest req){
		HttpSession session=req.getSession();
		UserAndSection attribute = (UserAndSection) session.getAttribute("obj");
		lea.setL_user(attribute.getUname());
		lea.setL_dept_id(attribute.getUdepartment_id());
		System.out.println(lea);
		 applyService.addLeaves(lea);
		return "index";
	}
	
	//新增用章申请
	@RequestMapping("/addOrders")
	public String addOrders(HttpServletRequest request,Orders order){
	
		HttpSession session=request.getSession();
		UserAndSection att=(UserAndSection)session.getAttribute("obj");
		order.setO_user(att.getUname());
		order.setO_dept_id(att.getUdepartment_id());
		order.setO_state("未审核");
		sealService.addOrders(order);
		System.out.println(order);
		return "Template/sealmanage";
	}
	
	//显示用章表
	@RequestMapping("/showOrderList")
	@ResponseBody
	public Map<String, Object> showOrderList(int currentPage){
		Page<PackageInfo> page=PageHelper.startPage(currentPage,3);
		List<Orders> showOrderList = sealService.showOrderList();
		
		Map<String, Object> map=new HashMap<>();
		map.put("order", showOrderList);
		map.put("count", page.getPages());
		map.put("currentPage", currentPage);
		return map;
	}
	
	//删除用章信息
	@RequestMapping("/deleteOrder")
	@ResponseBody
	public int deleteOrder(int O_id,HttpServletRequest req){
		int deleteOrder = sealService.deleteOrder(O_id);
		return deleteOrder;
	}
	
	//根据用章id查询所有信息
	@RequestMapping("/queryByIdOrder")
	@ResponseBody
	public Map<String, Object> queryByIdOrder(int O_id){
		Orders byIdOrder = sealService.queryByIdOrder(O_id);
		Map<String, Object> map=new HashMap<>();
		map.put("orders", byIdOrder);
		System.out.println(byIdOrder);
		return map;
	}
	
	//修改信息
	@RequestMapping("/modityOrder")
	@ResponseBody
	public int modityOrder(HttpServletRequest request,Orders order){
	
		int moifyOrder = sealService.moifyOrder(order);
		return moifyOrder;
	}
	
	//显示所有论坛信息
	@RequestMapping("/showPost")
	@ResponseBody
	public Map<String, Object> showPost(){
		List<Send> showPost = postService.showPost();
		Map<String, Object> map=new HashMap<>();
		map.put("showpost", showPost);
		System.out.println(map);
		return map;
	}
	
	//显示版块下拉框
	@RequestMapping("/showBored")
	@ResponseBody
	public Map<String, Object> showBored(){
		List<Send> showBored = postService.showBored();
		Map<String, Object> map=new HashMap<>();
		map.put("showBored", showBored);
		System.out.println("bored"+map);
		return map;
	}
	
	//新增帖子
	@RequestMapping("/addPost")
	@ResponseBody
	public Map addPost(Send s,HttpServletRequest request){
		//根据版本id查找name
		Bored bored = postService.queryByIdSend(s.getBor_id());
		s.setLookcount("0");
		s.setPtboard(s.getBor_id());
		s.setState("未审批");
		s.setMaster(bored.getBor_owner());
		//timestamp转为String
		SimpleDateFormat df=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		Timestamp now=new Timestamp(System.currentTimeMillis());
		String str=df.format(now);
		s.setPosted(str);
		int r=postService.addPost(s);	
		Map<String, Object> map=new HashMap<>();
		map.put("map", r);
		return map;
	}
	
	//按条件查询
	@RequestMapping("/querySends")
	@ResponseBody
	public Map<String,Object> querySend(int bor_id,String ptmain,int currentPage,String startTime,String endTime){
		System.out.println(startTime+"dd"+endTime);
		Map<String, Object> map=new HashMap<>();
		map.put("bor_id", bor_id);
		map.put("ptmain", ptmain);
		map.put("startTime", startTime);
		map.put("endTime", endTime);
		Page<PageInfo> page=PageHelper.startPage(currentPage, 8);
		List<Send> querySend = postService.querySend(map);

		Map<String, Object> map1=new HashMap<>();
		map1.put("count", page.getPages());
		map1.put("querysends", querySend);
		
		return map1;
	}
	
	//删除  批量删除
	@RequestMapping("/deletePost")
	@ResponseBody
	public int deletePost(String str){	
		System.out.println("str"+str);
		String[] ptids=str.split(",");
		System.out.println("ptid"+ptids);
		for (int i = 0; i < ptids.length; i++) {
			int sid=Integer.parseInt(ptids[i]);
			System.out.println("ptids[i]"+ptids[i]);
		postService.deleteSend(sid);
		}
		return 1;
	
			
	}
	//修改编辑内容
	@RequestMapping("/ModifyPost")
	@ResponseBody
	public int ModifyPost(Send s,ModelMap map){
		int modifyPost = postService.modifyPost(s);
		return modifyPost;
	}

	
	
	//查看帖子详细信息 获取id  传入前端
	@RequestMapping("/lookPost")
	public String lookPost(@RequestParam(value = "ptid") int ptid,Model map){
		System.out.println("dddddd");
		System.out.println("ptid"+ptid);
		map.addAttribute("ptid", ptid);
		return "Template/reply";
	}
	
	
	//新增回帖
	@RequestMapping("/addReplyPost")
	@ResponseBody
	public int addReplyPost(Reply r,HttpServletRequest req){
		System.out.println(r.getRtid()+r.getRtcontent()+r.getReowner());
		//回复人为自己  从对象里取出
		HttpSession session = req.getSession();
		UserAndSection att = (UserAndSection)session.getAttribute("obj");
		r.setRtname(att.getUname());
		//timestamp转为String
		SimpleDateFormat df=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		Timestamp now=new Timestamp(System.currentTimeMillis());
		String str=df.format(now);
		System.out.println("时间"+str);
		r.setRtdate(str);
		int addReplyPost = postService.addReplyPost(r);
		return addReplyPost;
	}
			//查看帖子详细信息
			@RequestMapping("/lookPost1")
			@ResponseBody
			public Map<String,Object> lookPost1(int ptid,int currentPage){
				System.out.println("dddddd");
				System.out.println("ptid"+ptid);
				System.out.println(currentPage+"currentPage");
				Map<String, Object> map=new HashMap<>();
				// 根据id查找主贴信息 
				Send lookMainPost = postService.lookMainPost(ptid);
				map.put("lookMainPost", lookMainPost);
				/*Page<PageInfo> page=PageHelper.startPage(currentPage, 6);*/
				List<Send> lookPost = postService.lookPost(ptid);
				for (Send send : lookPost) {
					System.out.println(send.getBor_name()+"mmm"+send.toString());
				}
				map.put("lookPost", lookPost);
			//	map.put("count", page.getPages());
				
				System.out.println(lookMainPost+"lookMainPost");
				System.out.println(lookPost+"look");
				return map;
			}
			//查找显示待审批的帖子
			@RequestMapping("/queryWaitPost")
			@ResponseBody
			public Map<String, Object> queryWaitPost(int bor_id,String ptmain,int currentPage,String startTime,String endTime){
				Map<String,Object> map=new HashMap<>();
				map.put("bor_id", bor_id);
				map.put("ptmain", ptmain);
				map.put("startTime", startTime);
				map.put("endTime", endTime);
				Page<PageInfo> page=PageHelper.startPage(currentPage, 5);
				List<Send> queryWaitPost = postService.queryWaitPost(map);
				System.out.println("hhhh"+queryWaitPost);
				Map<String, Object> map1=new HashMap<>();
				map1.put("count", page.getPages());
				map1.put("queryWaitPost", queryWaitPost);
				return map1;
			}
			//修改批准状态
			@RequestMapping("/modifyState")
			@ResponseBody
			public int modifyState(int ptid){
				 postService.modifyApprove(ptid);
				 return 1;	 
			}
			//查询展示我发布的帖子
			@RequestMapping("/queryShowSend")
			@ResponseBody
			public Map<String, Object> queryShowSend(HttpServletRequest req,int bor_id,String ptmain,int currentPage,String endTime,String startTime){
				Map<String, Object> map1=new HashMap<>();
				HttpSession session = req.getSession();
				UserAndSection att = (UserAndSection)session.getAttribute("obj");
				map1.put("author_id", att.getUname());
				map1.put("bor_id", bor_id);
				map1.put("ptmain", ptmain);
				map1.put("endTime", endTime);
				map1.put("startTime", startTime);
				Page<PageInfo> page=PageHelper.startPage(currentPage, 3);
				List<Send> showQuerySend = postService.showQuerySend(map1);
				Map<String, Object> map=new HashMap<>();
				map.put("meAppSend", showQuerySend);
				map.put("count", page.getPages());
				return map;
			}
			
			//	//显示论坛管理版块
			@RequestMapping("/showBoredPost")
			@ResponseBody
			public Map<String,Object> showBoredPost(int currentPage,int pagecount){
				Page<PageHelper> page=PageHelper.startPage(currentPage, pagecount);
				List<Bored> showBoredPost = postService.showBoredPost();
				System.out.println("showBoredPost"+showBoredPost);
				Map<String, Object> map=new HashMap<>();
				map.put("boredPost", showBoredPost);
				map.put("count", page.getPages());
				return map;
			}
			
			//删除版块
			@RequestMapping("deleteBord")
			@ResponseBody
			public int deleteBord(int bor_id){
				System.out.println("bor_id"+bor_id);
				int deleteRored = postService.deleteRored(bor_id);
				return deleteRored;
			}
			
			//添加版块
			/*@RequestMapping("/addBored")
			@ResponseBody
			public int addBored(HttpServletRequest req,Bored bored,String bor_name,int bor_style){
				HttpSession session = req.getSession();
				UserAndSection attribute = (UserAndSection)session.getAttribute("obj");
				String uname = attribute.getUname();
				bored.setBor_owner(uname);
				bored.setBor_nameid(uname);
				bored.setBor_style(bor_style);
				bored.setBor_name(bor_name);
				int addBored = postService.addBored(bored);
				return addBored;
			}*/
			//新增多条
			@RequestMapping("/addBored")
			@ResponseBody
			public int addBored(HttpServletRequest req,Bored bored,String str,String str2){
				String[] split = str.split(",");
				String[] split2 = str2.split(",");
				for (int i = 0; i < split2.length; i++) {
					if(!split2[i].equals("undefined")){
						List<Bored> list=new ArrayList<>();
						int parseInt = Integer.parseInt(split2[i]);
						bored.setBor_name(split[i]);
						bored.setBor_style(parseInt);
						HttpSession session = req.getSession();
						UserAndSection attribute = (UserAndSection)session.getAttribute("obj");
						String uname = attribute.getUname();
						bored.setBor_owner(uname);
						bored.setBor_nameid(uname);
						list.add(bored);
						postService.addBoreds(list);
					}	
				}
				return 1;
			}	
			//显示
			@RequestMapping("/queryRroed")
			@ResponseBody
			public Map queryRroed(){
				List<Bored> queryBored = postService.queryBored();
				Map<String, Object> map=new HashMap<>();
			map.put("Bored", queryBored);
			System.out.println(queryBored+"queryBored");
				return map;
			}
			
			//查询所有角色
			@ResponseBody
			@RequestMapping("/selectAllRoleList")
			public Map<String, Object> selectAllRoleList(){
				List<Role> allRoleList = systemServices.selectAllRoleList();
					HashMap<String, Object> map = new HashMap<String, Object>();
					map.put("allRoleList", allRoleList);
					return map;
					};
					//查询二级的权限和三级权限
					@RequestMapping("/selectPriviege")
				public String selectPriviege(int roleid,Model model){
						System.out.println("roleid"+roleid);
					Role selectRoleById = systemServices.selectRoleById(roleid);
					
					List<String> selectCheckedPrivilegeId = systemServices.selectCheckedPrivilegeId(roleid);
					System.out.println(selectRoleById+"selectCheckedPrivilegeId11"+selectCheckedPrivilegeId);
					model.addAttribute("selectRoleById", selectRoleById);
					model.addAttribute("checkedPrivilegeId", selectCheckedPrivilegeId);
					return "Template/Privilege";
				}
					//查找所有的权限
					@RequestMapping("/selectPrivilegeTopList")
					@ResponseBody
					public Map<String,Object> selectPrivilegeTopList(){
						Map<String,Object> map=new HashMap<>();
						List<Privilege> selectPrivilegeTopList = systemServices.selectPrivilegeTopList();
						System.out.println(selectPrivilegeTopList+"selectPrivilegeTopList");
						Iterator<Privilege> iterator = selectPrivilegeTopList.iterator();
						int  parent[]=new int[selectPrivilegeTopList.size()];
						int index=0;
						int index2=0;
						//二级类权限
						while (iterator.hasNext()) {
							Privilege privilege = iterator.next();
							parent[index++] = privilege.getChildren();
							System.out.println(privilege.getChildren());
						}
						//三级权限
						List<Privilege> selectPrivilegeById = systemServices.selectPrivilegeById(parent);
						Iterator<Privilege> iterator2 = selectPrivilegeById.iterator();
						int children[]=new int[selectPrivilegeById.size()];
						while (iterator2.hasNext()) {
							Privilege next = iterator2.next();
							children[index2++]= next.getChildren();	
						}
						List<Privilege> selectPrivilegeById2 = systemServices.selectPrivilegeById(children);
						System.out.println(selectPrivilegeById2+"selectPrivilegeById2");
						map.put("selectPrivilegeTopList", selectPrivilegeTopList);
						map.put("privilegeById", selectPrivilegeById);
						map.put("selectPrivilegeChil", selectPrivilegeById2);
						return map;
					}
					//新增权限
					@RequestMapping("/ainsertPrivilegeIds")
					@ResponseBody
					public String ainsertPrivilegeIds(RolePrivilege role,String str){	
						systemServices.deletePrivilege(role.getRoleId());
						System.out.println(role.getRoleId()+"用户Idppp"+str);
						if(str!=""){
							String[] split = str.split(",");
							for (int i = 0; i < split.length; i++) {
								if(!split[i].equals("undefined")){
								role.setPrivilegeId(Integer.parseInt(split[i]));
								systemServices.ainsertPrivilegeIds(role);
								}
							}
					}return "Template/Privilege";
}
}